package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.MyResult;
import com.api.shopapi.entity.PreCheck;
import com.api.shopapi.entity.Staff;
import com.api.shopapi.entity.User;
import com.api.shopapi.service.StaffService;
import com.api.shopapi.service.UserService;
import com.api.shopapi.util.QRCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;

@RestController
@RequestMapping("QRCode")
public class QRCodeController extends BaseController {


    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;


    @Autowired
    @Qualifier("UserServiceImplOne")
    private UserService userService;

    @RequestMapping(value = "GetImg",method = RequestMethod.POST)
    public MyResult GetImg(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck=checkSignAdmInterface(timeStr,sign,staffCode,"/QRCode/GetImg");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }


            Staff staff = staffService.getByStaffCode(staffCode);
            String governmentCode = staff.getGovernmentCode();
            String text=jumpPath+"?governmentCode="+governmentCode;
            String imgPath=keepPath+"/"+governmentCode+".png";

            File imgFile=new File(imgPath);
            if (imgFile.exists()){//判断二维码图片是否存在
                return success("\\qrCodes\\"+governmentCode+".png");
            }
            File file =new File(keepPath);
            if  (!file .exists()  && !file .isDirectory()){//判断保存二维码图片文件夹是否存在
                file .mkdir();
            }
            QRCodeUtil.generateQRCodeImage(text,200,200,imgPath);
            return success("\\qrCodes\\"+governmentCode+".png");
        } catch (Exception e) {
            insertError("/QRCode/GetImg",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "AppGetImg",method = RequestMethod.POST)
    public MyResult AppGetImg(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");

            PreCheck preCheck=checkSignFrontInterface(timeStr,sign,"/QRCode/AppGetImg");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }

            User user = userService.getByUserCode(userCode);
            String governmentCode = user.getGovernmentCode();
            String text=jumpPath+"?governmentCode="+governmentCode;
            String imgPath=keepPath+"/"+governmentCode+".png";

            File imgFile=new File(imgPath);
            if (imgFile.exists()){//判断二维码图片是否存在
                JSONObject jsonObject1=new JSONObject();
                jsonObject1.put("imgUrl","\\qrCodes\\"+governmentCode+".png");
                jsonObject1.put("jumpUrl",text);
                return success(jsonObject1);
            }
            File file =new File(keepPath);
            if  (!file .exists()  && !file .isDirectory()){//判断保存二维码图片文件夹是否存在
                file .mkdir();
            }

            QRCodeUtil.generateQRCodeImage(text,200,200,imgPath);
            JSONObject jsonObject1=new JSONObject();
            jsonObject1.put("imgUrl","\\qrCodes\\"+governmentCode+".png");
            jsonObject1.put("jumpUrl",text);
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/QRCode/AppGetImg",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


}
